<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2018/4/20
 * Time: 14:24
 */

namespace app\mobile\controller;
use think\Loader;
use  \think\Session;
use \think\Request;
use think\Log;
use  app\common\logic\TaobaoLogic;

class Taobao extends MobileBase {

    public function _initialize() {
        Loader::import('taobao\TopSdk', EXTEND_PATH);

        $c = new \TopClient;
        $c->appkey = '24798316';
        $c->secretKey = 'b60868c5ab3157899d9b0cfa05325669';
        $this->TopClient=$c;

    }

    public function index()
    {
      $cc=new \my\ff();
      echo   $cc->aa();
    }

    /*
     * 单品搜索
     *
     * 链接或者地址搜索
     */

 public function searchOne(){
     $str = "【tefeishi美容仪器嫩肤导入仪按摩家用脸部去皱导出排毒射频童颜机】http://m.tb.cn/h.WCUlx7b 点击链接，再选择浏览器打开；或复制这条信息￥2T6S0H7XXV9￥后打开手淘";


     $tb_api=new TaobaoLogic();
     $keywords=input('post.q','');

//     $keywords=$str;
     $data=array();
     $data=$tb_api->optionalFinddetail($keywords);

$this->assign('keywords',$keywords);
$this->assign('data',$data);

return $this->fetch();
 }

 public function test(){

     return $this->fetch();
 }

// public function orders()
// {
//
//     $req = new TbkOrderGetRequest;
//     $req->setFields("tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time,tk3rd_pub_id,tk3rd_site_id,tk3rd_adzone_id");
//     $req->setStartTime("2016-05-23 12:18:22");
//     $req->setSpan("600");
//     $req->setPageNo("1");
//     $req->setPageSize("20");
//     $req->setTkStatus("1");
//     $req->setOrderQueryType("settle_time");
//     $resp = $this->TopClient->execute($req);
//     print_r($resp);
// }

public function  ordrerlist(){
    $req = new \TbkOrderGetRequest;
    $req->setFields("tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time,tk3rd_pub_id,tk3rd_site_id,tk3rd_adzone_id");
    $req->setStartTime("2018-04-23 12:18:22");
    $req->setSpan("600");
    $req->setPageNo("1");
    $req->setPageSize("20");
    $req->setTkStatus("1");
    $req->setOrderQueryType("settle_time");
    $resp = $this->TopClient->execute($req);
    print_r($resp);
}

 public function goods()
    {

        $req = new \TbkItemGetRequest;
        $req->setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,seller_id,volume,nick");
//        $req->setQ("https://item.taobao.com/item.htm?id=557160071773&systype=m&from=tbkfenxiangplus&fromScene=100&tbkShareId=4009561344&ali_trackid=2%3Amm_26632441_0_0%3A1524276509963_260_4159937212&sourceType=item&suid=1e616c31-61be-4610-8a6d-22fd534117d5&shareid=c1ce7918-3e97-4948-a188-5295c35d2bcc&ut_sk=1.WmnIXFbpi10DAJqAi0YoInpB_21646297_1524276497073.Copy.baobeixiangqingfenxiang&un=b1abecf67801ac5e132b0f6991806569&share_crt_v=1&cpp=1&shareurl=true&spm=a313p.22.ha.942621132199&short_name=h.WxVTjOr&app=chrome");
        $req->setQ("春夏薄款运动裤 高端定织褶皱弹力面料 男士休闲裤抽绳卫裤长裤");
//        $req->setCat("16,18");
//        $req->setItemloc("杭州");
//        $req->setSort("tk_rate_des");
//        $req->setIsTmall("false");
//        $req->setIsOverseas("false");
//        $req->setStartPrice("10");
//        $req->setEndPrice("10");
//        $req->setStartTkRate("123");
//        $req->setEndTkRate("123");
//        $req->setPlatform("1");
//        $req->setPageNo("123");
//        $req->setPageSize("20");

        print_r(Collection($this->TopClient->execute($req))->toArray())   ;


    }

    /*
     * taobao.tbk.item.info.get (淘宝客商品详情（简版）)
     * 正式环境	http://gw.api.taobao.com/router/rest	https://eco.taobao.com/router/rest
     * 沙箱环境	http://gw.api.tbsandbox.com/router/rest	https://gw.api.tbsandbox.com/router/rest
     *
     */

    public function goodsinfo(){




        $req = new \TbkItemInfoGetRequest;
        $req->setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url");
        $req->setPlatform("1");

        $ids=input('?get.ids');
        $req->setNumIids("557160071773");
        echo '<pre>';
        print_r(Collection($this->TopClient->execute($req))->toArray())   ;
    }

    /*
     * taobao.tbk.coupon.get (阿里妈妈推广券信息查询)
     * 正式环境	http://gw.api.taobao.com/router/rest	https://eco.taobao.com/router/rest
     * 沙箱环境	http://gw.api.tbsandbox.com/router/rest	https://gw.api.tbsandbox.com/router/rest
     *
     */
    public function copon(){

        Loader::import('taobao\TopSdk', EXTEND_PATH);
        $c = new \TopClient;
        $c->appkey = $this->APP_KEY;
        $c->secretKey =  $this->SECRETKEY;

        $req = new \TbkCouponGetRequest;
        $req->setMe("nfr546473874087%2BYTo2k1PX18gaNN%2BIPkIG2PadNYbBnwEsv6mRavWieOoOE3L9OdmbDSSyHbGxBAXjHpLKvZbL1320ML%2BCF5FRtW7N7yJ056Lgym4X01A%3D");
        $req->setItemId("40276935723");
        $req->setActivityId("sdfwe3eefsdf");
        print_r(Collection($c->execute($req))->toArray())   ;

    }


    public function order(){
        Loader::import('TopSdk', EXTEND_PATH);
        $c = new \TopClient;
        $c->appkey = $this->APP_KEY;
        $c->secretKey =  $this->SECRETKEY;
        $req = new \TbkScNewuserOrderGetRequest;
        $req->setPageSize("20");
        $req->setAdzoneId("123");
        $req->setPageNo("1");
        $req->setSiteId("123");
        if(!Session::has('session')){
                         Session::set('session','6102129eb02058c255f0dbc717979c87b9826136ff99136425137072','think');
                         Session::set('refresh_token','6100e292eea238ed3398f8b2f529a7f67a0b4138d4d74ab425137072','think');

        }


        print_r(Collection($c->execute($req,$sessionKey))->toArray())   ;
    }



    public function spread_get(){
        Loader::import('TopSdk', EXTEND_PATH);
        $c = new \TopClient;
        $c->appkey = $this->APP_KEY;
        $c->secretKey =  $this->SECRETKEY;
        $req = new \TbkSpreadGetRequest;
        $requests = new \TbkSpreadRequest;
        $requests->url="http://uland.taobao.com";
        $req->setRequests(json_encode($requests));
        $resp = $c->execute($req);

       print_r($resp);
    }
    /*
     * 	mm_17620797_42568708_237658530
     */
     public function  get_link(){

         $item_id=input('get.item_id');

        $data=[
            'token'=>'7000210130535629810ba8f38d5c3a9a59c8965c64730b60e4ada3ac9babcd4f1affc80425137072',
//            'item_id'=>'43299076947',
            'item_id'=>$item_id,
//            'item_id'=>'565037588364',
            'adzone_id'=>'237658530',
            'site_id'=>'42568708',
            'qq'=>'610841147'
        ];


//        $http='http://193.112.121.99/xiaocao/hightapi_wuquan.action';
        $http='http://193.112.121.99/xiaocao/hightapi.action';
         $result_data=array();

       $result_data=json_decode($this->curl_request($http,$data),true);
//       echo $this->curl_request($http,$data);
         echo '<pre>';
       print_r($result_data);

         Log::info('日志信息');

    }
    //参数1：访问的URL，参数2：post数据(不填则为GET)，参数3：提交的$cookies,参数4：是否返回$cookies
    private function curl_request($url,$post='',$cookie='', $returnCookie=0){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
        if($post) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
        }
        if($cookie) {
            curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        }
        curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        if($returnCookie){
            list($header, $body) = explode("\r\n\r\n", $data, 2);
            preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
            $info['cookie']  = substr($matches[1][0], 1);
            $info['content'] = $body;
            return $info;
        }else{
            return $data;
        }
    }
public function tpwd_creat()
{
    Loader::import('taobao\TopSdk', EXTEND_PATH);
    $c = new \TopClient;
    $c->appkey = $this->APP_KEY;
    $c->secretKey =  $this->SECRETKEY;
    $req = new \TbkTpwdCreateRequest;
    $req->setUserId("123");
    $req->setText("长度大于5个字符");
    $req->setUrl("https://uland.taobao.com/coupon/edetail?e=CUT9KR6%2BYxcGQASttHIRqVhZCepEnK7JoyQ2XNykG%2FDd%2FNb7reXTt%2BBGlf5r9Zzpc2pTwomJjVlMK6VTtTXHpZQ5wfGz%2Fu%2BNAtY9WDAhVfOWENi52CLtZrEsC315c4LYwrB2l0ebRG2z%2BEEIX1kmAg%3D%3D&traceId=0bba623415245345021686778e");
    $req->setLogo("https://uland.taobao.com/");
    $req->setExt("{}");
    $resp = $c->execute($req);
    echo '<pre>';
    print_r($resp)  ;
}
}